#include "ZLabel.h"
#include "ZPaintManager.h"
#include "ZControlFactory.h"

namespace ZuiLib
{

ZLabel::ZLabel(void)
	:m_dwTextColor(0xff000000), 
	m_dwDisabledTextColor(0xff808080),
	m_bAutoWidth(false)
{
	m_bMouseEnabled=false;
	SetRectEmpty(&m_rcTextPadding);
	m_dwTextStyle = 0;
	m_nFontSize = _dpi_scale(DEFAULT_FONT_SIZE);
}

ZLabel::~ZLabel(void)
{
}


const char* ZLabel::GetClass() const
{
	return "Label";
}


void* ZLabel::GetInterface(const char* pstrName)
{
	if( strcmp(pstrName,"Label") == 0 ) 
		return static_cast<ZLabel*>(this);
	else
		return ZControl::GetInterface(pstrName);
}

void ZLabel::SetTextStyle(uint32_t uStyle)
{
    m_dwTextStyle = uStyle;
    Invalidate();
}


uint32_t ZLabel::GetTextStyle() const
{
	return m_dwTextStyle;
}

void ZLabel::SetTextColor(color_t dwTextColor)
{
	m_dwTextColor = dwTextColor;
	Invalidate();
}


color_t ZLabel::GetTextColor() const
{
	return m_dwTextColor;
}
void ZLabel::SetDisabledTextColor(color_t dwTextColor)
{
	m_dwDisabledTextColor = dwTextColor;
}
color_t ZLabel::GetDisabledTextColor() const
{
	return m_dwDisabledTextColor;
}
void ZLabel::SetFontName(const char* font)
{
    m_sFont = font;
}

const char* ZLabel::GetFontName()
{
	return m_sFont;
}
RECT ZLabel::GetTextPadding() const
{
	return m_rcTextPadding;
}
void ZLabel::SetTextPadding(RECT rc)
{
	m_rcTextPadding=rc;
}

void ZLabel::SetAutoWidth(bool autoWidth) {
	m_bAutoWidth = autoWidth;
	NeedParentUpdate(); 
}

SIZE ZLabel::EstimateSize(SIZE szAvailable)
{
	if (m_bAutoWidth)
	{
		ZFreeTypeFontPtr font = GetRenerder()->FindFont(m_sFont, m_nFontSize);
		SIZE sz;
		sz.cx = font->GetWidth(m_sText) + 
				m_rcTextPadding.left + m_rcTextPadding.right;
		sz.cy = m_cxyFixed.cy;
		return sz;
	}
	else
		return ZControl::EstimateSize(szAvailable);
}


void ZLabel::DoEvent(TEventUI& event)
{
    if( event.Type == UIEVENT_SETFOCUS ) 
    {
        m_bFocused = true;
        return;
    }
    if( event.Type == UIEVENT_KILLFOCUS ) 
    {
        m_bFocused = false;
        return;
    }
    if( event.Type == UIEVENT_MOUSEENTER )
    {
        return;
    }
    if( event.Type == UIEVENT_MOUSELEAVE )
    {
        return;
    }
    ZControl::DoEvent(event);
}


void ZLabel::SetAttribute(const char* pstrName, const char* pstrValue)
{
	if(stricmp(pstrName, "align") == 0 ) {
		m_dwTextStyle = TextToDrawType(pstrValue);
	}
	else if(stricmp(pstrName, "endellipsis") == 0 )
	{
		if(stricmp(pstrValue, "true") == 0 )
			m_dwTextStyle |= DT_END_ELLIPSIS;
		else 
			m_dwTextStyle &= ~DT_END_ELLIPSIS;
	}    
	else if (stricmp(pstrName, "font") == 0) {
		SetFontName(pstrValue);
	}
	else if (stricmp(pstrName, "fontsize") == 0)
	{
		m_nFontSize = ParserSize(pstrValue);
	}
	else if(stricmp(pstrName, "textcolor") == 0 )
	{
		SetTextColor(TextToColor(pstrValue));
	}
	else if(stricmp(pstrName, "disabledtextcolor") == 0 )
	{
		SetDisabledTextColor(TextToColor(pstrValue));
	}
	else if (stricmp(pstrName, "autowidth") == 0)
	{
		SetAutoWidth(stricmp(pstrValue, "true") == 0);
	}
	else if(stricmp(pstrName, "textpadding") == 0 )
	{
		RECT rcTextPadding;
		ParserRect(&rcTextPadding, pstrValue);
		SetTextPadding(rcTextPadding);
	}
	else
		ZControl::SetAttribute(pstrName, pstrValue);
}

void ZLabel::PaintText(ZRenderer* render)
{
	if( m_dwTextColor == 0 ) 
		m_dwTextColor = m_pManager->GetDefaultFontColor();

	if( m_dwDisabledTextColor == 0 )
		m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();

	RECT rc = m_rcItem;
	InsetRect(&rc, &m_rcTextPadding);
	color_t color=m_dwTextColor;

	if( !IsEnabled() ) {
		color=m_dwDisabledTextColor;
	}
	render->DrawText(m_sFont, m_sText.GetString(), m_nFontSize, rc, color,m_dwTextStyle);
}


ZFreeTypeFontPtr ZLabel::GetFont()
{
	return GetRenerder()->FindFont(m_sFont,m_nFontSize);
}



ZString ZLabel::GetText() const
{
	return m_sText;
}


void ZLabel::SetText(const char* pstrText)
{
	m_sText = pstrText;
	Invalidate();
}

}//namespace
